;;; Comment:
;;;
;;; This is an experimental implementation of the binary substituter for
;;; substitutes over GNunet, using scripts from the ‘scheme-gnunet’ library.
;;;
;;; Code:

;; XXX unadvisable
(eval-when (expand load eval)
  (add-to-load-path (string-append (getenv "HOME")
                                   "/guix/git/scheme-gnunet")))

(define-module (guix scripts substitute gnunet)
  #:export (gnunet-substituter)
  #:use-module (guix scripts substitute)
  #:use-module (gnu gnunet scripts download-store)
  #:use-module (guix narinfo)
  #:use-module (guix records))

(define (download-store . args)
  (apply system* "guile" "-L"
         (string-append (getenv "HOME") "/guix/git/scheme-gnunet")
         "-e" "(@ (gnu gnunet scripts download-store) main)"
         args))

(define (narinfo-gnunet-uri narinfo)
  (assoc-ref (recutils->alist
              (open-input-string (narinfo-contents narinfo)))
             "GNUnet"))

(define (process-substitution/gnunet destination narinfo . rest)
  (let* ((gnunet-uri (narinfo-gnunet-uri narinfo)))
    (if gnunet-uri
        (begin (with-output-to-port (current-error-port)
                 (lambda ()
                   (download:gnunet-nar/0-to-fs gnunet-uri destination)))
               'unpacked)
        #f)))

(define gnunet-substituter
  (make-substituter 'gnunet process-substitution/gnunet
                    (const '()) ; XXX fetch-narinfos is broken when a narinfo hook is #f
                    '(http https file))) ; oops let's rework this

